(function($){
	$.fn.ueditor = function(){
		if(typeof arguments[0] == "string"){
			if($.fn.ueditor.methods[arguments[0]]){
				return $.fn.ueditor.methods[arguments[0]].apply(this,Array.prototype.slice.call(arguments,1));
			}
			return UM.getEditor(this.attr("uid"))[arguments[0]].apply(UM.getEditor(this.attr("uid")),Array.prototype.slice.call(arguments,1));
		}
		var opt = {};
		
		opt = $.extend(true,opt,$.fn.validatebox.defaults,$.fn.ueditor.defaults,arguments[0]);
		opt.withoutIll = true;
		var edit = "addContent"+Math.ceil(Math.random()*100);
		this.parent().removeClass("col-1-3").addClass("col-3-3");
		var ele = $('<script name="'+this.attr("name")+'" type="text/plain" id="'+edit+'" style="width:'+opt.width+';"> </script> ');
		ele.insertAfter(this);
		this.attr("ueditorname",this.attr("name")).attr("uid",edit).removeAttr("name").hide();
		
		var um = UM.getEditor(edit,$.extend(true,{
			imageUrl:BASEPATH+"/base/authoriza/fileup/uploadFile.htm",
			imgInsertAfter:opt.imgInsertAfter,
			uploadFilter:function(data){
				if(typeof data == "string"){
					var res = data.match(/>(.*?)</);
					if(res&&res.length>1){
						data = res[1];
					}
					data = eval("("+data+")").data;
				}else{
					data = data.data;
				}
				return  '{ name:"'+data.fileName+'", originalName:"'+data.fileName+'", size: "'+data.fileSize+'", state: "SUCCESS", type:".'+data.fileExt+'", url: "'+BASEPATH+"/download/file/"+data.filePath+data.fileId+"."+data.fileExt+'" }';
			}
		},opt.ueopt));
		um.setHeight(opt.height);
		opt.ueditor = edit;
		if(opt.validType){
			opt.novalidate=false;
			$.fn.ueditor.methods.validate.call(this,this,opt);
		}
		document.onkeydown=function(e){
			if(um.isFocus()){
				event.returnValue=true;
			}
		}
		if(opt.required){
			$("#"+edit).addClass("validatebox-text");
		}
		$.data($("#"+edit)[0],"validatebox",{options:opt,textbox:this});
	}
	$.fn.ueditor.defaults = {
		onBeforeValidate:function(){
			$(this).val = function(){
				$.fn.val = function(){
					return this.innerHTML;
				}
				return this.html();
			}
		},
		width:'100%',
		height:250,
		ueopt:{},
		imgInsertAfter:function(arg){
		},
		onValidate:function(isok){
			var box;
			if(this.toString().indexOf("Div")!=-1){
				box = $(this);
			}else{
				box = $("#"+$(this).attr("uid"));
			}
			isok = false;
			box = box.closest(".edui-container").prev();
			$.fn.ueditor.methods.validate.call(box,box,$.data(this,"validatebox").options);
		}
	}
	$.fn.ueditor.methods = {
		options:function(jq){
			return $.data($("#"+this.attr("uid"))[0],"validatebox").options;
		},
		validate:function(jq){
		},
		readonly:function(arg){
			if(arg==false){
				this.removeAttr("disable");
				UM.getEditor(this.attr("uid")).setEnabled();
			}else{
				if(this instanceof Window){
					return ;
				}
				this.attr("disable","disabled");
				UM.getEditor(this.attr("uid")).setDisabled('fullscreen');
			}
			var val = $("textarea[name='"+this.attr("ueditorname")+"']").val();
			UM.getEditor(this.attr("uid")).setContent(val,false);
		},
		setValue:function(value){
			if(this instanceof Window){
				return ;
			}
			if(value.indexOf("<body")!=-1){
				if(value.indexOf("<script")!=-1)
					value = value.substring(value.indexOf('body>')+5,value.indexOf("<script"));
				else
					value = value.substring(value.indexOf('body>')+5,value.indexOf("</body>"));
			}
			if(value==undefined) value="";
			var div = $("<div style='display:none'>"+value+"</div>").appendTo("body");
			div.find("*[tsrc]").each(function(){
				$(this).attr("src",BASEPATH+$(this).attr("tsrc"));
			});
			var html = div.html();
			var ueditor = UM.getEditor(this.attr("uid"));
			ueditor.addListener("ready", function () {
				ueditor.setContent(html,false);
			});
			if(ueditor.isReady==1){
				ueditor.setContent(html,false);
			}
			this.value=html;
			return ueditor;
		},
		getValue:function(){
			return UM.getEditor(this.attr("uid")).getContent();
		}
	}
})($);